package series;

import javax.sql.DataSource;

import org.apache.commons.dbcp.BasicDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;

@Configuration
@ImportResource("classpath:properties-config.xml")
public class SeriesConfiguration {

	@Value("${jdbc.driver}")
	private String driver;

	@Value("${jdbc.url}")
	private String url;

	@Value("${jdbc.username}")
	private String username;

	@Value("${jdbc.password}")
	private String password;

	@Bean
	public EpisodeDAO episodeDAO() {
		return new EpisodeDAO(jdbc());
	}

	@Bean
	public SeasonDAO seasonDAO() {
		return new SeasonDAO(jdbc(), episodeDAO());
	}

	@Bean
	public SeriesDAO seriesDAO() {
		return new SeriesDAO(jdbc(), seasonDAO());
	}

	@Bean
	public JdbcTemplate jdbc() {
		return new JdbcTemplate(dataSource());
	}

	// @Bean
	// public DataSource dataSource() {
	// return new DriverManagerDataSource(url, username, password);
	// }

	@Bean
	public javax.sql.DataSource dataSource() {
		BasicDataSource bds = new BasicDataSource();
		bds.setDriverClassName(driver);
		bds.setUrl(url);
		bds.setUsername(username);
		bds.setPassword(password);
		return bds;
	}
}
